package com.qlylkj.lbk.ui.company

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.qlylkj.lbk.base.BaseViewModel
import com.qlylkj.lbk.model.api.ResultResponse
import com.qlylkj.lbk.model.bean.Company
import com.qlylkj.lbk.model.repository.CommonRepository
import com.qlylkj.lbk.model.repository.CompanyRepository
import java.io.File

class CompanyViewModel : BaseViewModel() {

    private val _uiState = MutableLiveData<CompanyUiModel>()
    val uiState: LiveData<CompanyUiModel>
        get() = _uiState

    private val _uploadUiState = MutableLiveData<UploadUiModel>()
    val uploadUiState: LiveData<UploadUiModel>
        get() = _uploadUiState

    private val commonRepository by lazy { CommonRepository() }

    private val companyRepository by lazy { CompanyRepository() }

    private fun emitUiState(
        showError: String? = null,
        company: Company? = null,
        enterCompanyInfoSuccess: Boolean = false,
        editCompanyInfoSuccess: Boolean = false,
        companyFromCoChainSuccess: Boolean = false
    ) {
        val uiModel = CompanyUiModel(showError, company, enterCompanyInfoSuccess,editCompanyInfoSuccess,companyFromCoChainSuccess)
        _uiState.value = uiModel
    }

    private fun uploadEmitUiState(
        showError: String? = null,
        type: String = "",
        uploadUrl: String? = null
    ) {
        val uiModel = UploadUiModel(showError, type, uploadUrl)
        _uploadUiState.value = uiModel
    }


    data class CompanyUiModel(
        val showError: String?,
        val company: Company?,
        val enterCompanyInfoSuccess: Boolean,
        val editCompanyInfoSuccess: Boolean,
        val companyFromCoChainSuccess: Boolean
    )

    data class UploadUiModel(
        val showError: String?,
        val type: String,
        val uploadUrl: String?
    )

    fun getCompanyInfo(companyId: String) {
        launch {
            val result = companyRepository.getCompany(companyId)
            if (result is ResultResponse.SuccessEntity) {
                emitUiState(company = result.data)
            } else if (result is ResultResponse.Error) {
                emitUiState(showError = result.exception.message)
            }
        }
    }

    fun uploadFile(type: String, uploadFile: File) {
        launch {
            val result = commonRepository.uploadFile(uploadFile)
            if (result is ResultResponse.SuccessEntity) {
                uploadEmitUiState(type = type, uploadUrl = result.data)
            } else if (result is ResultResponse.Error) {
                uploadEmitUiState(showError = result.exception.message)
            }
        }
    }

    fun enterCompanyInfo(options: MutableMap<String,Any>){
        launch {
            val result = companyRepository.enterCompanyInfo(options)
            if (result is ResultResponse.Success) {
                emitUiState(enterCompanyInfoSuccess = true)
            } else if (result is ResultResponse.Error) {
                emitUiState(showError = result.exception.message)
            }
        }
    }

    fun editCompanyInfo(options: MutableMap<String,Any>){
        launch {
            val result = companyRepository.editCompanyInfo(options)
            if (result is ResultResponse.Success) {
                emitUiState(editCompanyInfoSuccess = true)
            } else if (result is ResultResponse.Error) {
                emitUiState(showError = result.exception.message)
            }
        }
    }

    fun companyFromCoChain(companyId:String){
        launch {
            val result = companyRepository.companyFromCoChain(companyId)
            if (result is ResultResponse.Success) {
                emitUiState(companyFromCoChainSuccess = true)
            } else if (result is ResultResponse.Error) {
                emitUiState(showError = result.exception.message)
            }
        }
    }

}